import json
import logging
from django.db import connection
from django.http import JsonResponse
from wxcloudrun import util


def create(request, _):
    print("创建报表")
    cursor = connection.cursor()
    cursor.callproc('makereport', [])  # 创建业务报表
    connection.connection.commit()
    print("创建报表完成")
    rsp = JsonResponse({'code': 0, 'errorMsg': '', 'result': '创建报表完成'}, json_dumps_params={'ensure_ascii': False})
    return rsp


def getresult(request, _):
    body_unicode = request.body.decode('utf-8')
    body = json.loads(body_unicode)
    if 'action' not in body:
        return JsonResponse({'code': -1, 'errorMsg': '缺少action参数'},
                            json_dumps_params={'ensure_ascii': False})
    rsp = JsonResponse({'code': 0, 'errorMsg': '', 'data': ""},
                       json_dumps_params={'ensure_ascii': False})
    if body['action'] == 'report':
        jsonvalues = util.getresult("call getreport(" + body['reportId'] + ");")
        rsp = JsonResponse({'code': 0, 'errorMsg': '', 'data': jsonvalues},
                       json_dumps_params={'ensure_ascii': False})
    return rsp
